<?php
class UserController extends Zend_Controller_Action
{
    private function getDb ()
    {
        $dbAdapter = Zend_Controller_Front::getInstance()->getParam('bootstrap')
            ->getPluginResource('db')
            ->getDbAdapter();
        return $dbAdapter;
    }
    public function init ()
    {
        Zend_Session::start();
        /* Initialize action controller here */
    }
    public function indexAction ()
    {
        $this->_helper->layout->disableLayout();
    }
    public function registerAction () // 注册
    {
        // $this->_helper->layout->disableLayout();
    }
    public function checkloginAction ()
    {
        $this->_helper->layout->disableLayout();
        $username = $_POST["username"];
        $password = $_POST["password"];
        // $captcha=$_POST["captcha"];
        // echo $username;
        // echo $password;
        if ($username != NULL && $password != NULL) {
            $validator = new Zend_Validate_Alnum();
            if ($validator->isValid($username)) {
                $db = $this->getDb();
                $authAdapter = new Zend_Auth_Adapter_DbTable($db);
                $authAdapter->setTableName('user')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('password')
                    ->setCredentialColumn('regactive')
                    ->setIdentity($username)
                    ->setCredential($password)
                    ->setCredential('1');
                $auth = Zend_Auth::getInstance();
                $authResult = $auth->authenticate($authAdapter);
                $result = $authResult->isValid();
                if ($result == true) {
                    echo 1;
                } else {
                    echo 0;
                }
                ;
            } else {
                echo 0;
            }
        } else {
            echo 0;
        }
    }
    public function loginuserAction ()
    { // 显示用户名
        $username = Zend_Auth::getInstance()->getIdentity();
        $this->view->user = $username;
    }
    public function loginsuccessAction ()
    {
        $username = Zend_Auth::getInstance()->getIdentity();
        if (isset($username)) {
            $this->_redirect('../a.html');
        } else {
            echo "请登录";
        }
    }
    public function loginoutAction ()
    { // 注销
        Zend_Auth::getInstance()->clearIdentity();
        $reurl = $_SERVER['HTTP_REFERER'];
        $this->_redirect($reurl);
    }
    public function regactivateAction ()
    {
        $userData = $this->_request->getParams();
        $username = $userData['username'];
        $password = $userData['pass'];
        $emali = $userData['email'];
        $userID = $userData['id'];
        $telephone = $userData['telephone'];
        $sex = $userData['sex'];
        $address = $userData['address'];
        if (empty($username)) {
            $this->_redirect('/index/index');
            die(); // 如果侵入直接返回首页
        }
        function checkUsername ($name)
        { // 验证用户名
            if (preg_match("/^\\w+$/", $name)) {
                return $name;
            } else {
                return FALSE;
            }
        }
        $validatorUsername = checkUsername($username);
        function checkPassword ($passw)
        { // 验证密码
            $validatorPass = new Zend_Validate_StringLength(6, 50);
            if ($validatorPass->isValid($passw)) {
                return $passw;
            } else {
                return false;
            }
        }
        $validatorPassword = checkPassword($password);
        function checkEmail ($userEmail)
        { // 验证邮箱
            $validator = new Zend_Validate();
            $validator->addValidator(new Zend_Validate_StringLength(4, 50))->addValidator(
            new Zend_Validate_EmailAddress());
            if ($validator->isValid($userEmail)) {
                return $userEmail;
            } else {
                return false;
            }
        }
        $validatorEmail = checkEmail($emali);
        // echo $validatorEmail;
        // Zend_Debug::dump($validatorEmail);
        function checkUserID ($id)
        { // 验证身份证
            if (preg_match("/^\\d{15,18}$/", $id)) {
                return $id;
            } else {
                return false;
            }
        }
        $validatorID = checkUserID($userID);
        // Zend_Debug::dump($validatorID);
        function checkUserTel ($telNUM)
        { // 验证电话
            if (preg_match("/^\\d{8,12}$/", $telNUM)) {
                return $telNUM;
            } else {
                return false;
            }
        }
        $validatorTel = checkUserTel($telephone);
        if ($validatorUsername && $validatorPassword && $validatorID &&
         $validatorTel && $validatorEmail) {
            $userDb = new Application_Model_userAdd();
            $randNUM = rand(1, 500);
            $mo5Rand = md5($randNUM);
            $userRow = array('username' => $validatorUsername, 
            'password' => $validatorPassword, 'userID' => $validatorID, 
            'email' => $validatorEmail, 'telephone' => $validatorTel, 
            'randNUM' => $mo5Rand);
            // Zend_Debug::dump($userRow);
            $t = $userDb->insert($userRow);
            $activateURL = "http://www.a.com/user/regsuccess?username=" .
             $username . "&" . "rand=" . $mo5Rand;
            $email = new Zend_Mail('utf-8');
            $stmpEmail = new Zend_Mail_Transport_Smtp('smtp.163.com', 
            array('name' => 'smtp.163.com', 
            'username' => 'wanghao850707@163.com', 'password' => '19850707', 
            'auth' => 'login'));
            $email->addTo($validatorEmail, 'ninhao')
                ->setFrom('wanghao850707@163.com', 'wanghao')
                ->setSubject('预约挂号注册确认')
                ->setBodyText($activateURL)
                ->setBodyHtml(
            "亲爱的" . $validatorUsername .
             "：<Br />
<br />
欢迎加入预约挂号网!<br />
请点击链接完成注册：<br />
<br /><br />" .
             $activateURL . "<br>如果您的email程序不支持链接点击，请将上面的地址拷贝至您的浏览器(如IE)的地址栏进入预约挂号。<br />

希望您在预约挂号的体验有益和愉快!<br /><br />


- 预约挂号网<br /><br />

(这是一封自动产生的email，请勿回复。)<br />

----------------------------------------------------------------------
              ")
                ->send($stmpEmail);
            $this->view->email = $validatorEmail;
        } else {
            echo "不对";
        }
    }
    public function regsuccessAction ()
    {
        $username = $_GET['username'];
        $rand = $_GET['rand'];
        $db = $this->getDb();
        $select = $db->select();
        $sql = $select->from('user', 'username')
            ->where('randNUM=?', $rand)
            ->where('username=?', $username);
        $result = $db->fetchOne($sql);
        if ($result != false) {
            $table = new Application_Model_userAdd();
            $db = $table->getAdapter();
            $set = array('regactive' => 1);
            $where = $db->quoteInto('username=?', $result);
            $row = $table->update($set, $where);
            $this->view->sussecss = '注册成功';
        } else {
            $this->view->sussecss = '注册失败';
        }
    }
}

